import { defineStore } from "pinia";
import {
    reqMajorAll,
    reqMajorList,
    reqMajorAdd,
    reqMajorById,
    reqMajorUpdata,
    reqMajorDelete
} from "@/api/major"
import type {
    majorInfoData,
    majorListFormData,
    majorResponseData,
    majorAllResponseData,
    majorListResponseData
} from "@/api/major/type"
import { MajorState } from "./types/type";
let useMajorStore = defineStore("Major", {
    state: (): MajorState => {
        return {
            majors: []
        }
    },
    actions: {
        // 总
        async getMajorAll() {
            let result: majorAllResponseData = await reqMajorAll();
            if (result.status === 0) {
                this.majors = (result.data as majorInfoData[])
                return "ok"
            } else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 部分
        async getMajorList(data: majorListFormData) {
            let result: majorListResponseData = await reqMajorList(data);
            if (result.status === 0) {
                this.majors = (result.data?.data as majorInfoData[])
                return result.data
            } else {
                return Promise.reject(new Error(result.msg));
            }
        },
        // 添加
        async addmajor(data: majorInfoData) {
            let result: majorResponseData = await reqMajorAdd(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // ID获取
        async getMajorById(_id: string) {
            let result: majorResponseData = await reqMajorById(_id)
            if (result.status === 0) {
                return result.data
            } else {
                return Promise.reject(new Error(result.msg))

            }
        },
        // 修改
        async updateMajor(data: majorInfoData) {
            let result: majorResponseData = await reqMajorUpdata(data);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除
        async deleteMajor(majorId: string) {
            let result: majorResponseData = await reqMajorDelete(majorId);
            if (result.status == 0) {
                return "OK"
            }
            else {
                return Promise.reject(new Error(result.msg))
            }
        },
        getters: {

        }
    }

})
export default useMajorStore